package cn.idietrade.idietradegoodsmodule8002.controller;

import cn.idietrade.common.pojo.SecondImg;
import cn.idietrade.common.utils.ReturnResultData;
import cn.idietrade.idietradegoodsmodule8002.service.imgservice.SecondImgService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.logging.Logger;

/**
 * @author zjy
 * @version 1.0
 * @createTime 2022-12-09 15:55
 * @description
 */
@Slf4j
@RestController
public class SecondImgController {
    @Resource
    private SecondImgService secondImgService;

    Logger logger =Logger.getLogger("SecondImgController");

    @RequestMapping("/addImg")
    public ReturnResultData addImg(SecondImg secondImg){
        ReturnResultData resultData = new ReturnResultData<>();
        try {
            int i = secondImgService.addImg(secondImg);
            if(i>0){
                resultData.setCode(1);
                resultData.setMessage("图片添加成功");
                resultData.setData(null);
            }else{
                resultData.setCode(0);
                resultData.setMessage("图片添加失败");
                resultData.setData(null);
                logger.info("图片添加失败");
            }
        }catch (Exception e){
            resultData.setCode(-1);
            resultData.setMessage("图片添加异常");
            resultData.setData(null);
            logger.info("图片添加异常");
        }
        return resultData;
    }

    @RequestMapping("/queryImgAll")
    public ReturnResultData queryImgAll(Integer pageNum, Integer pageSize,Integer goodsId){
        ReturnResultData<PageInfo<SecondImg>> resultData = new ReturnResultData<>();
        try {
            PageInfo<SecondImg> imgPageInfo = secondImgService.queryImgAll(pageNum, pageSize, goodsId);
            if(imgPageInfo!=null){
                resultData.setCode(1);
                resultData.setMessage("图片查询成功");
                resultData.setData(imgPageInfo);
            }else{
                resultData.setCode(0);
                resultData.setMessage("图片查询失败");
                resultData.setData(null);
                logger.info("图片查询失败");
            }
        }catch (Exception e){
            resultData.setCode(-1);
            resultData.setMessage("图片查询异常");
            resultData.setData(null);
            logger.info("图片查询异常");
        }
        return resultData;
    }

    @RequestMapping("/updataImg")
    public String updataImg(Integer goodsId,String ids){
        int i = secondImgService.updataImg(goodsId, ids);
        if (i>0){
            return "成功";
        }else{
            return "失败";
        }
    }


}
